package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j

public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    //分页查询
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            String name,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Page<Dish> page = dishService.findByPage(name, pageNum, pageSize);
        return ResultInfo.success(page);
    }

    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }

    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        //后台新增菜品，redis就要删除一次，
        String key="dish_"+dish.getCategoryId();
        redisTemplate.delete(key);
        dishService.save(dish);
        return ResultInfo.success(null);
    }

    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish) {
        //更新操作可能会更改菜品分类导致影响到多个菜品，所以要把redis的缓存都删掉
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishService.update(dish);
        return ResultInfo.success(null);
    }

    @DeleteMapping("/dish")
    public ResultInfo deleteByIds(@RequestParam("ids") List<Long> ids) {
        //删除操作可能会更改菜品分类导致影响到多个菜品，所以要把redis的缓存都删掉
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        if (ids != null && ids.size() > 0) {
            dishService.deleteByIds(ids);
        }


        return ResultInfo.success(null);
    }

    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status") Integer status, @RequestParam("ids") List<Long> ids) {
        //状态操作可能会更改菜品分类导致影响到多个菜品，所以要把redis的缓存都删掉
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        if (ids != null && ids.size() > 0) {
            dishService.updateStatus(status, ids);
        }

        return ResultInfo.success(null);
    }

    //根据分类id和status=1查询菜品信息
//    @GetMapping("/dish/list")
//    public ResultInfo findList(Long categoryId, @RequestParam(defaultValue = "1") Integer status) {
//        List<Dish> dishList = dishService.findList(categoryId, status);
//        return ResultInfo.success(dishList);
//    }
    @GetMapping("/dish/list")
    public ResultInfo findList(Long categoryId, @RequestParam(defaultValue = "1") Integer status, String name) {
        List<Dish> dishList = dishService.findList(categoryId, status, name);
        return ResultInfo.success(dishList);
    }


}
